package com.blensmile.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.blensmile.R;


/**
 * Created by Blensmile on 2017/1/16.
 */
public class BorderRoundImageView extends ImageView {

    private static final @ColorInt
    int MASKING_COLOR = Color.TRANSPARENT;

    private int borderRadius = 5;
    private int borderWidth = 1;
    private @ColorInt
    int borderColor = 0xffcacaca;
    private @ColorInt
    int maskingColor = 0;
    private int width;
    private int height;
    private Paint rectPaint;
    private Paint borderPaint;
    private Paint bitmapPaint;
    private Paint maskingPaint;

    public BorderRoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public BorderRoundImageView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public BorderRoundImageView(Context context) {
        this(context,null);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BorderRoundImageView);
            borderRadius = a.getDimensionPixelSize(R.styleable.BorderRoundImageView_BorderRadius, borderRadius);
            borderWidth = a.getDimensionPixelSize(R.styleable.BorderRoundImageView_BorderWidth, borderWidth);
            borderColor = a.getColor(R.styleable.BorderRoundImageView_BorderColor, borderColor);
            maskingColor = a.getColor(R.styleable.BorderRoundImageView_MaskingColor,MASKING_COLOR);
        } else {
            float density = context.getResources().getDisplayMetrics().density;
            borderRadius = (int) (borderRadius * density);
            borderWidth = (int)(borderWidth *density);
        }

        bitmapPaint = new Paint();
        bitmapPaint.setXfermode(null);

        rectPaint = new Paint();
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(borderColor);
        rectPaint.setAntiAlias(true);
        rectPaint.setStrokeWidth(borderWidth);
        rectPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setStyle(Paint.Style.STROKE);

        maskingPaint = new Paint();
        maskingPaint.setAntiAlias(true);
        maskingPaint.setColor(maskingColor);
        maskingPaint.setStyle(Paint.Style.FILL);

        int border = borderWidth;
        setPadding(getPaddingLeft()+border,getPaddingTop()+border,getPaddingRight()+border,getPaddingBottom()+border);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    public void draw(Canvas canvas) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(bitmap);
        super.draw(canvas2);
        drawRoundRect(canvas2);
        canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
        drawMasking(canvas);
        drawEdge(canvas);
        bitmap.recycle();
    }

    private void drawEdge(Canvas canvas2) {
        canvas2.drawRoundRect(getRectF(getPaddingLeft(),getPaddingTop(),width-getPaddingRight(),height-getPaddingBottom()), borderRadius, borderRadius,borderPaint);
    }

    private void drawRoundRect(Canvas canvas) {
        Path path = new Path();
        path.addRoundRect(new RectF(getPaddingLeft(),getPaddingTop(),width-getPaddingRight(),height-getPaddingBottom()),borderRadius,borderRadius, Path.Direction.CW);
        path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
        canvas.drawPath(path, rectPaint);
    }

    private void drawMasking(Canvas canvas){
        Path path = new Path();
        path.addRoundRect(new RectF(getPaddingLeft(),getPaddingTop(),width-getPaddingRight(),height-getPaddingBottom()),borderRadius,borderRadius, Path.Direction.CW);
        canvas.drawPath(path, maskingPaint);
    }

    private RectF getRectF(int l, int t, int r, int b){
        int pen  = borderWidth /2;
        return new RectF(l-pen,t-pen,r+pen,b+pen);
    }

    public void setMaskingColor(@ColorInt int maskingColor){
        if(maskingColor != this.maskingColor) {
            this.maskingColor = maskingColor;
            maskingPaint.setColor(maskingColor);
            invalidate();
        }
    }

}

